package cn.yunhe.services.impl;

import cn.yunhe.dao.IProblemDao;
import cn.yunhe.entity.Problem;
import cn.yunhe.services.IProblemServices;
import cn.yunhe.util.StringToMap;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/5
 */
@Service
public class ProblemServices implements IProblemServices {

    @Resource
    private IProblemDao problemDao;

    /**
     * 查询全部试题
     *
     * @param cond
     * @return
     */
    @Override
    public Page queryListLikeProblem(Map<String, Object> cond) {
        Page page = new Page();
        //得到总条数
        page.setTotal(problemDao.getLikeProblemCount(cond));
        //每页大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);
        //当前页码
        int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setPageNum(curPageNum);
        //将条件传递给pageHelper
        page = PageHelper.startPage(curPageNum, pageSize);
        List<Problem> list = problemDao.getLikeProblem(cond);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setPro_mapname(StringToMap.stringToMap(list.get(i).getPro_choice()));
        }
        return page;
    }
    /**
     * 根据id试题查看
     */
    public Problem findProblem(int pro_id) {

        return problemDao.findProblem(pro_id);
    }

    /**
     * 添加试题
     * @param problem
     * @return
     */
    @Override
    public int insertProblemServices(Problem problem) {

        return problemDao.insertProblem(problem);
    }
    /**
     * 修改试题
     */
    public int updateProblem(Problem problem) {
        return problemDao.updateProblem(problem);
    }

    /**
     * 删除试题
     */
    public int deleteProblem(Problem problem){

        return problemDao.deleteProblem(problem);
    }
    @Override
    public int getTypeProblemCount(int id) {

        return problemDao.getTypeProblemCount(id);
    }

    @Override
    public int getTypeAndPointCount(Map map)
    {
        return problemDao.getTypeAndPointCount(map);
    }
}
